package dao

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/sqlc"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"go-admin/base/utils"
	"strings"
)

var _ UserRoleModel = (*customUserRoleModel)(nil)

type (
	// UserRoleModel is an interface to be customized, add more methods here,
	// and implement the added methods in customUserRoleModel.
	UserRoleModel interface {
		userRoleModel
		FindUserRolesByUserId(ctx context.Context, userId int64) ([]*UserRole, error)
		DeleteByIds(ctx context.Context, ids []int64) error
		DeleteByIdsSession(ctx context.Context, session sqlx.Session, ids []int64) error
		InsertSession(ctx context.Context, session sqlx.Session, role *UserRole) error
		FindUserRolesByUserIds(ctx context.Context, userIds []int64) ([]*UserRole, error)
		FindUserRolesByRoleId(ctx context.Context, roleId int64) ([]*UserRole, error)
		FindUserRolesByRoleIds(ctx context.Context, roleIds []int64) ([]*UserRole, error)
		BatchInsert(ctx context.Context, conn sqlx.SqlConn, userRoles []*UserRole) error
	}

	customUserRoleModel struct {
		*defaultUserRoleModel
	}
)

func (c customUserRoleModel) BatchInsert(ctx context.Context, conn sqlx.SqlConn, userRoles []*UserRole) error {
	query := fmt.Sprintf("insert into %s (%s) values (?, ?)", c.table, userRoleRowsExpectAutoSet)
	bulkInserter, _ := sqlx.NewBulkInserter(conn, query)
	defer bulkInserter.Flush()
	for _, userRole := range userRoles {
		err := bulkInserter.Insert(userRole.UserId, userRole.RoleId)
		if err != nil {
			return err
		}
	}
	return nil
}

func (c customUserRoleModel) InsertSession(ctx context.Context, session sqlx.Session, userRole *UserRole) error {
	query := fmt.Sprintf("insert into %s (%s) values (?, ?)", c.table, userRoleRowsExpectAutoSet)
	_, err := session.ExecCtx(ctx, query, userRole.UserId, userRole.RoleId)
	return err
}

func (c customUserRoleModel) DeleteByIdsSession(ctx context.Context, session sqlx.Session, ids []int64) error {
	query := fmt.Sprintf("delete from %s where `id` in ( %s )", c.table, strings.Join(utils.GeneratePlaceholderByInt64(ids), ","))
	_, err := session.ExecCtx(ctx, query, utils.ConvertToAny(ids)...)
	return err
}

func (c customUserRoleModel) DeleteByIds(ctx context.Context, ids []int64) error {
	query := fmt.Sprintf("delete from %s where `id` in ( %s )", c.table, strings.Join(utils.GeneratePlaceholderByInt64(ids), ","))
	_, err := c.conn.ExecCtx(ctx, query, utils.ConvertToAny(ids)...)
	return err
}

func (c customUserRoleModel) FindUserRolesByUserIds(ctx context.Context, userIds []int64) ([]*UserRole, error) {
	//query := fmt.Sprintf("select %s from %s where `user_id` in ( ? )", userRoleRows, c.table)
	query := fmt.Sprintf("select %s from %s where `user_id` in ( %s )", userRoleRows, c.table, strings.Join(utils.GeneratePlaceholderByInt64(userIds), ","))
	var resp []*UserRole
	err := c.conn.QueryRowsCtx(ctx, &resp, query, utils.ConvertToAny(userIds)...)
	switch err {
	case nil:
		return resp, nil
	case sqlc.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

func (c customUserRoleModel) FindUserRolesByRoleIds(ctx context.Context, roleIds []int64) ([]*UserRole, error) {
	//query := fmt.Sprintf("select %s from %s where `role_id` in ( ? )", userRoleRows, c.table)
	query := fmt.Sprintf("select %s from %s where `role_id` in ( %s )", userRoleRows, c.table, strings.Join(utils.GeneratePlaceholderByInt64(roleIds), ","))
	var resp []*UserRole
	err := c.conn.QueryRowsCtx(ctx, &resp, query, utils.ConvertToAny(roleIds)...)
	switch err {
	case nil:
		return resp, nil
	case sqlc.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

func (c customUserRoleModel) FindUserRolesByUserId(ctx context.Context, userId int64) ([]*UserRole, error) {
	query := fmt.Sprintf("select %s from %s where `user_id` = ?", userRoleRows, c.table)
	var resp []*UserRole
	err := c.conn.QueryRowsCtx(ctx, &resp, query, userId)
	switch err {
	case nil:
		return resp, nil
	case sqlc.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

func (c customUserRoleModel) FindUserRolesByRoleId(ctx context.Context, roleId int64) ([]*UserRole, error) {
	query := fmt.Sprintf("select %s from %s where `role_id` = ?", userRoleRows, c.table)
	var resp []*UserRole
	err := c.conn.QueryRowsCtx(ctx, &resp, query, roleId)
	switch err {
	case nil:
		return resp, nil
	case sqlc.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

// NewUserRoleModel returns a model for the database table.
func NewUserRoleModel(conn sqlx.SqlConn) UserRoleModel {
	return &customUserRoleModel{
		defaultUserRoleModel: newUserRoleModel(conn),
	}
}
